<?php
/**
 * Flea Site
 *
 * @copyright  Copyright (c) 2008 Movoin Studio (http://movoin.com)
 * @license    Apache License 2.0
 * @version    $Id: Products.php 130 2009-05-21 14:23:24Z movoin $
 */

//{{include
FLEA::loadClass('Controller_Base');
//}}

class Controller_Products extends Controller_Base
{
    var $modelProducts;

    function Controller_Products()
    {
        parent::Controller_Base();
        $this->modelProducts = & FLEA::getSingleton( 'Model_Products' );
    }

    /**
     * 产品列表
     *
     */
    function actionIndex()
    {
        $this->_setBack();
        $data['focus'] = 'column';
        $data['column'] = $this->getColumnInf( $_GET[FLEA::getAppInf( 'columnAccessor' )] );

        $table = & $this->modelProducts->getTable();
        $table->enableLinks(array('category','attachments'));
        $where = array(
            array( 'col_key', $_GET[FLEA::getAppInf( 'columnAccessor' )] ),
            array( 'site_id', (int)$_GET['site'] )
            );
        if ($_GET['cate_id']) {
            $where[] = array('cate_id', (int)$_GET['cate_id']);
        }
        FLEA::loadHelper( 'pager' );
        $page = isset($_GET['page']) ? (int)$_GET['page'] : 0;
        $pagesize = 10;
        $pager =& new FLEA_Helper_Pager($table, $page, $pagesize, $where, 'sort_id ASC, id DESC');
        $data['pager'] = $pager->getPagerData();
        $data['rows'] = $pager->findAll('id,cate_id,title,views_count,created');
        /**
         * 公共助手
         *
         */
        FLEA::loadFile( 'Helper_Common' );
        $this->_executeView('products', $data);
    }

    /**
     * 添加产品
     *
     */
    function actionAdd()
    {
        $this->_setBack();
        $table =& $this->modelProducts->getTable();
        $row = $this->_prepareData($table->meta);
        $this->_editProduct($row);
    }

    /**
     * 修改产品
     *
     */
    function actionEdit()
    {
        $this->_setBack();
        $pro_id = $_GET['id'];
        if(!$pro_id) {
            js_alert('非法操作!', 'history.back(-1)');
        }
        $row = $this->modelProducts->getContent((int)$pro_id, 'id, cate_id, title, content', 'attachments');
        $this->_editProduct(&$row);
    }

    /**
     * 排序产品
     *
     */
    function actionSort()
    {
        $this->_setBack();
        $data['focus'] = 'column';
        $data['column'] = $this->getColumnInf( $_GET[FLEA::getAppInf( 'columnAccessor' )] );
        $this->_executeView('content-sort', $data);
    }

    /**
     * 保存产品排序结果
     *
     */
    function actionSaveSort()
    {
        js_alert($this->modelProducts->sortProducts(&$_POST['seqNoList']), 0, $this->_getBack());
    }

    /**
     * 删除产品
     *
     */
    function actionRemove()
    {
        $pro_id = $_GET['id'];
        if(!$pro_id) {
            js_alert('非法操作!', 'history.back(-1)');
        }
        js_alert($this->modelProducts->removeProduct((int)$pro_id), 0, $this->_getBack());
    }

    /**
     * 保存产品
     *
     */
    function actionSaveProduct()
    {
        js_alert($this->modelProducts->saveProduct($_POST, 'pic'), 0, $this->_getBack());
    }

    /**
     * 产品分类
     *
     */
    function actionCategories()
    {
        $this->_setBack();

        $data['focus'] = 'column';
        $data['column'] = $this->getColumnInf( $_GET[FLEA::getAppInf( 'columnAccessor' )] );
        $categories = $this->modelProducts->getCategories($_GET[FLEA::getAppInf( 'columnAccessor' )], null, 'cate_id,parent_id,name', 'attachments');
        /**
         * 公共助手
         *
         */
        FLEA::loadFile( 'Helper_Common' );
        $data['rows'] = composeCategories($categories);
        $this->_executeView('products-categories', $data);
    }

    /**
     * 添加产品分类
     *
     */
    function actionAddCategory()
    {
        $this->_setBack();
        $table =& $this->modelProducts->getTable('category');
        $row = $this->_prepareData($table->meta);
        $this->_editCategory(&$row);
    }

    /**
     * 修改产品分类
     *
     */
    function actionEditCategory()
    {
        $this->_setBack();
        $cate_id = $_GET['cate_id'];
        if(!$cate_id) {
            js_alert('非法操作!', 'history.back(-1)');
        }
        $row = $this->modelProducts->getCategory((int)$cate_id, 'cate_id, parent_id, name, description', 'attachments');
        $this->_editCategory(&$row);
    }

    /**
     * 保存产品分类
     *
     */
    function actionSaveCategory()
    {
        js_alert($this->modelProducts->saveCategory($_POST, 'pic'), 0, $this->_getBack());
    }

    /**
     * 产品分类排序
     *
     */
    function actionSortCategory()
    {
        $this->_setBack();
        $data['focus'] = 'column';
        $data['column'] = $this->getColumnInf( $_GET[FLEA::getAppInf( 'columnAccessor' )] );
        $this->_executeView('category-sort', $data);
    }

    /**
     * 保存排序结果
     *
     */
    function actionSaveCategorySort()
    {
        js_alert($this->modelProducts->sortCategories(&$_POST['seqNoList']), 0, $this->_getBack());
    }

    /**
     * 删除产品分类
     *
     */
    function actionRemoveCategory()
    {
        $cate_id = $_GET['cate_id'];
        if(!$cate_id) {
            js_alert('非法操作!', 'history.back(-1)');
        }
        js_alert($this->modelProducts->removeCategory((int)$cate_id), 0, $this->_getBack());
    }

    /**
     * 编辑产品
     *
     */
    function _editProduct(&$row)
    {
        $data['focus'] = 'column';
        $data['column'] = $this->getColumnInf( $_GET[FLEA::getAppInf( 'columnAccessor' )] );
        $categories = $this->modelProducts->getCategories($_GET[FLEA::getAppInf( 'columnAccessor' )], null, 'cate_id,parent_id,name');
        /**
         * 公共助手
         *
         */
        FLEA::loadFile( 'Helper_Common' );
        $data['categories'] = composeCategories($categories);
        $data['row'] = $row;
        $this->_executeView('products-edit', $data);
    }

    /**
     * 编辑产品分类
     *
     */
    function _editCategory(&$row)
    {
        $data['focus'] = 'column';
        $data['column'] = $this->getColumnInf( $_GET[FLEA::getAppInf( 'columnAccessor' )] );
        $data['categories'] = $this->modelProducts->getCategories($_GET[FLEA::getAppInf( 'columnAccessor' )], 0, 'cate_id,name');
        $data['row'] = $row;
        $this->_executeView('products-categories-edit', $data);
    }

}
